#define  _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;


typedef int ElemType;
typedef struct ss_table {
	ElemType* data;
	int TableLen;
}SS_table;


void StInite(SS_table& ST, int tabelen)
{

	ST.TableLen = tabelen;
	ST.data = (ElemType*)malloc(sizeof(ElemType)*ST.TableLen);
	srand((unsigned)time(NULL));
	for (int i = 0; i < ST.TableLen; i++)
	{
		ST.data[i] = rand() % 100;
	}
}

void PrintSt(SS_table ST)
{
	for (int i = 0; i < ST.TableLen; i++)
	{
		printf("%3d", ST.data[i]);
	}
	printf("\n");
}


int Binar_search(SS_table ST, ElemType key)
{
	int left = 0;
	int right = ST.TableLen - 1;

	while (left <= right)
	{
		int mid = (right + left) / 2;
		if (ST.data[mid] <key)
		{
			left = mid+1;
		}
		else if (ST.data[mid] > key)
		{
			right = mid - 1;
		}
		else
		{
			return mid;
		}
	}
	return -1;
}

int compare(const void* left, const void* right)
{
	return *(ElemType*)left - *(ElemType*)right;
}
int main()
{
	SS_table ST;
	StInite(ST, 10);
	PrintSt(ST);

	qsort(ST.data, ST.TableLen, sizeof(ElemType), compare);
	/*sort(ST.data[0], ST.data[ST.TableLen], [](const int& u, const int& v)
	{
		return u > v; 
	});*/

	PrintSt(ST);

	ElemType key;
	printf("please input serach key:\n");
	scanf("%d", &key);
	int pos = 0;
	pos = Binar_search(ST, key);
	if (pos!=-1)
	{
		printf("find out,the pos:%d\n", pos);
	}
	else
	{
		printf("find failed\n");
	}
	return 0;
}